﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.Fields;

namespace XComplica.PublicStarter
{
    /// <summary>
    /// Class used to render a clean html version of the RichImage from SharePoint publishing.
    /// </summary>
    public class PublishingImage: CompositeControl
    {
        public string FieldName { get; set; }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(FieldName) && null != SPContext.Current && null != SPContext.Current.Item)
            {
                ImageFieldValue imgValue = SPContext.Current.Item[FieldName] as ImageFieldValue;
                if (null != imgValue)
                {
                    writer.WriteBeginTag("img");
                    writer.WriteAttribute("src", imgValue.ImageUrl);
                    writer.WriteAttribute("alt", imgValue.AlternateText);
                    //Add any attributes as desired
                    writer.Write(HtmlTextWriter.SelfClosingTagEnd);
                }
            }
        }   
    }
}
